//
//  GHHomeViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHHomeViewController.h"
#import "GHHomeHeaderView.h"
#import "GHHomeDataController.h"
#import "GHHomeCollectionViewCell.h"
#import "GHHomeControlTableViewCell.h"
#import "GHNewGoodsTableViewCell.h"
#import "GHKeepTableViewCell.h"
#import "GHGoodDetailViewController.h"
#define sectionFooterHeight 80
#define collectionViewItemHeight 300
@interface GHHomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong) GHHomeHeaderView                     *headerView;
@property(nonatomic, strong) UICollectionView                      *collectionView;
@property(nonatomic, strong) GHHomeTableData                        *tableData ;//tableView数据
@property(nonatomic, strong) NSArray<GHGoodModel*>                   *collectionViewData;
@property(nonatomic, strong) NSString                     *section1Image;
@property(nonatomic, strong) NSString                     *section2Image;
@end

@implementation GHHomeViewController

#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"神马国际";
    NSLog(@"shahe %@",NSHomeDirectory());
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.frame = CGRectMake(0, 0, UIScreenWidth, UIScreenHeight  - 64);
    if (!IOS11) {
        self.tableView.frame = CGRectMake(0, 0, UIScreenWidth, UIScreenHeight  - 64 - 44);
    }
    [self configSubView];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //[self reloadALLData];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.headerView.playeView pause];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    self.headerView = [[GHHomeHeaderView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 300)];
    self.tableView.tableHeaderView = self.headerView;
    
    UICollectionViewFlowLayout * flow = [UICollectionViewFlowLayout new];
    flow.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 900) collectionViewLayout:flow];
    self.collectionView.backgroundColor = GH_backgroundColor;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.scrollEnabled = NO;
    [self.collectionView registerClass:[GHHomeCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    self.tableView.tableFooterView = self.collectionView;
    
    //refresh
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self reloadALLData];
    }];
    [self.tableView.mj_header beginRefreshing];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    if (section == 1) {
        return self.tableData.listNew.count;
    }
    if (section == 2) {
        return self.tableData.listRec.count;
    }
    return 0 ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        NSInteger num = self.tableData.goodsTop.count / 4 + 1;
        return num * home_cell_controHeight;
    }
    if (indexPath.section == 1) {
        return [GHNewGoodsTableViewCell cellHeight];
    }
    if (indexPath.section == 2) {
        return [GHKeepTableViewCell cellHeight];
    }
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }
    return 200;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 200)];
    headerView.backgroundColor = self.tableView.backgroundColor;
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 190)];
    [headerView addSubview:imageView];
    if (section == 0 ) {
        imageView.image = [UIImage imageNamed:@"main-goods-5"];
        return [UIView new];
    }
    if (section == 1 ) {
        imageView.image = [UIImage imageNamed:@"section1"];
        if (self.section1Image.length) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.section1Image] placeholderImage:GH_placeHoderImage];
        }
    }
    if (section == 2 ) {
        imageView.image = [UIImage imageNamed:@"main-goods-2"];
        if (self.section2Image.length) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.section2Image] placeholderImage:GH_placeHoderImage];
        }
    }
    return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 2) {
        return sectionFooterHeight;
    }
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 2) {
        UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, sectionFooterHeight)];
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0.6*footer.st_width, 40)];
        imageView.centerX = footer.st_width / 2;
        imageView.centerY = footer.st_height / 2;
        imageView.image = [UIImage imageNamed:@"main_zutou"];
        [footer addSubview:imageView];
        return footer;
        
    }
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        static NSString * reuseIdentifier =  @"GHHomeControlTableViewCell";
        GHHomeControlTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (!cell) {
            cell = [[GHHomeControlTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
            
        }
        cell.array = self.tableData.goodsTop;
        __weak typeof(self) weakSelf =  self;
        [cell setButtonAction:^(GHGoodModel *model) {
            [weakSelf goDetailVcWithModel:model];
        }];
        return cell;
    }
    if (indexPath.section == 1) {
        static NSString * reuseIdentifier =  @"GHNewGoodsTableViewCell";
        GHNewGoodsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (!cell) {
            cell = [[GHNewGoodsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.tableData.listNew[indexPath.row];
        return cell;
    }
    if (indexPath.section == 2) {
        static NSString * reuseIdentifier =  @"GHKeepTableViewCell";
        GHKeepTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (!cell) {
            cell = [[GHKeepTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.tableData.listRec[indexPath.row];
        return cell;
    }
    return nil;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GHGoodModel * model ;
    if (indexPath.section == 1) {
        model = self.tableData.listNew[indexPath.row];
            [self goDetailVcWithModel:model];
    }
    if (indexPath.section == 2) {
        model = self.tableData.listRec[indexPath.row];
            [self goDetailVcWithModel:model];
    }

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma --mark UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.collectionViewData.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(UIScreenWidth/2 - 20, collectionViewItemHeight);
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GHHomeCollectionViewCell * item =[collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    item.model = self.collectionViewData[indexPath.row];
    return item;
}
#pragma --mark UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GHGoodModel * model = self.collectionViewData[indexPath.row];
    [self goDetailVcWithModel:model];
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
#pragma mark --Private Method
- (void)reloadALLData{
    [self send_news_request];
}
- (void)goDetailVcWithModel:(GHGoodModel*)model{
    GHGoodDetailViewController * vc = [GHGoodDetailViewController new];
    vc.goodID = model.goodId;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark --NetWork Method
- (void)send_news_request{
    //轮播
    [GHHomeDataController fetch_BannerDataWithHandle:^(NSArray<NSString *> *banners, NSString *section1Image, NSString *section2Image, STError *error) {
        if (banners.count) {
            self.headerView.adverView.imageUrlsArray = banners;
            self.section1Image  = section1Image;
            self.section2Image = section2Image;
            
        }
    }];
    //新闻
    [GHHomeDataController fetch_NewsDataWithHandle:^(NSArray<GHNewsModel *> *news, STError *error) {
        if (news.count) {
            NSMutableArray * test = [NSMutableArray new];
            [test addObjectsFromArray:news];
            // [test addObjectsFromArray:news];
            self.headerView.newsCirculView.dataSouce = test;
            [self.tableView reloadData];
        }

    }];
    //视屏
    [GHHomeDataController fetch_videoDataWithHandle:^(GHVideoModel *video, STError *error) {
        if (!error) {
            NSString * url = video.vedio_url;
            url =  [url stringByReplacingOccurrencesOfString:@".avi" withString:@".mp4"];
            video.vedio_url = url;
            self.headerView.videoModel = video;
            
        }
    }];
    //获取tabview 数据
    [GHHomeDataController fetch_tableViewDataWithHandle:^(GHHomeTableData *tableData, STError *error) {
        [self.tableView.mj_header endRefreshing];
        if (!error) {
            self.tableData = tableData;
            [self.tableView reloadData];
        }
    }];
    //底部所有商品
    [GHHomeDataController fetch_collectionView_goodsWithPage:1 Handle:^(NSArray<GHGoodModel *> *news, STError *error) {
        if (!error) {
            self.collectionViewData = [NSMutableArray new];
            self.collectionViewData = news;
            
            self.collectionView.height = (self.collectionViewData.count/2 + 1) * collectionViewItemHeight;
            [self.collectionView reloadData];
            self.tableView.tableFooterView = self.collectionView;
            [self.tableView reloadData];
        }
    }];
}

@end
